package tw.cating.Utility;

import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.view.View;
import android.webkit.WebView;

public class ADUtility {
    private static final String Addr = "http://61.220.104.63/~cating/adsense.html";
    private static final String AddrTest = "http://61.220.104.63/~cating/";
    
    public static Thread prepareADWebView(final Context mContext, final WebView v) {
    	Thread t = new Thread() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
                if (true == checkInternetConnection(mContext, AddrTest, "utf-8"))
                {
                        v.setVisibility(View.VISIBLE);
                        v.getSettings().setJavaScriptEnabled(true);
                        try {
                                v.loadUrl(Addr);
                        }catch(Exception e) 
                        {
                                
                        }
                }
			}
    	};
    	t.run();
    	return t;
    }
    
    protected static boolean checkInternetConnection(Context context, String strURL, String strEncoding) {
        int intTimeout = 5000; // 1000 = 1 ¬í
        try {
            HttpURLConnection urlConnection= null;
            URL url = new URL(strURL);
            urlConnection=(HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestProperty("User-Agent", context.getPackageName());
            urlConnection.setRequestProperty("Content-type","text/html; charset="+strEncoding);
            urlConnection.setConnectTimeout(intTimeout);
            urlConnection.connect();
            if (urlConnection.getResponseCode() == 200) {
                return true;
            } else {
                return false;
            }          
        } catch (Exception e) {
          e.printStackTrace();
          return false;
        }
    }
}
